package Project;
//___________________________IMPORTACIONES______________________________________

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
//______________________________________________________________________________

public class ReporteGrafCompa extends javax.swing.JDialog {
//_____________________________ATRIBUTOS________________________________________

    private IniciarReporte jasper;
    String base;
    int opcion;
    int ano1;
    int ano2;
    int ano3;
//_____________________________CONSTRUCTOR______________________________________

    public ReporteGrafCompa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jasper = new IniciarReporte();
        this.setLocationRelativeTo(rootPane);
        pnlCombobox1();
        llenarcomobo();
        llenarComboVarios();
    }
//________________________________METODOS_______________________________________

    private void llenarComboVarios() {
        BaseDeDatos.conectarse();
        try {
            Statement s = BaseDeDatos.con.createStatement();
            ResultSet rs = s.executeQuery("select * from tempo2 where ano < 2035");
            while (rs.next()) {
                cbxAno4.removeAllItems();
                base = rs.getString(1);
                cbxAno3.addItem(base);
                ano1 = Integer.parseInt(cbxAno3.getSelectedItem().toString());
                for (int i = ano1 + 1; i < 2036; i++) {
                    cbxAno4.addItem(i);
                }
                selectItem();
            }
            rs.close();
            s.close();
            BaseDeDatos.desconectarse();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        pack();
        setLocationRelativeTo(null);
    }
//______________________________________________________________________________

    private void llenarcomobo() {
        BaseDeDatos.conectarse();
        try {
            Statement s = BaseDeDatos.con.createStatement();
            ResultSet rs = s.executeQuery("select count(date_part('year',accidente.fecha)) as total, date_part('year',accidente.fecha) as fecha from accidente  group by date_part('year',accidente.fecha) order by fecha asc");
            while (rs.next()) {
                base = rs.getString(2);
                cbxAno1.addItem(base);
                cbxAno2.addItem(base);
            }
            rs.close();
            s.close();
            BaseDeDatos.desconectarse();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        pack();
        setLocationRelativeTo(null);
    }
//______________________________________________________________________________

    private void selectItem() {

        cbxAno3.addItemListener(new java.awt.event.ItemListener() {
            @Override
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxAno3ItemStateChanged(evt);
            }

            private void cbxAno3ItemStateChanged(ItemEvent evt) {
                ano1 = Integer.parseInt(cbxAno3.getSelectedItem().toString());
                cbxAno4.removeAllItems();
                for (int i = ano1 + 1; i < 2036; i++) {
                    cbxAno4.addItem(i);
                }
            }
        });
    }
//______________________________________________________________________________

    private void pnlCombobox2() {

        pnlContenedorCombobox.setLayout(new BorderLayout());
        pnlContenedorCombobox.removeAll();
        pnlContenedorCombobox.add(pnlCombobox2, "West");
        pnlContenedorCombobox.updateUI();

    }
//______________________________________________________________________________

    private void pnlCombobox1() {

        pnlContenedorCombobox.setLayout(new BorderLayout());
        pnlContenedorCombobox.removeAll();
        pnlContenedorCombobox.add(pnlCombobox1, "West");
        pnlContenedorCombobox.updateUI();

    }
//______________________________________________________________________________

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        pnlCombobox2 = new javax.swing.JPanel();
        cbxAno3 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cbxAno4 = new javax.swing.JComboBox();
        pnlCombobox1 = new javax.swing.JPanel();
        cbxAno1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbxAno2 = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        cbxGenerarReporte = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        radioDos = new javax.swing.JRadioButton();
        radioVarios = new javax.swing.JRadioButton();
        rbtnLineal = new javax.swing.JRadioButton();
        rbtnBarras = new javax.swing.JRadioButton();
        rbtnTorta = new javax.swing.JRadioButton();
        pnlContenedorCombobox = new javax.swing.JPanel();

        cbxAno3.setFont(new java.awt.Font("Arial", 0, 12));
        cbxAno3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Y");

        cbxAno4.setFont(new java.awt.Font("Arial", 0, 12));
        cbxAno4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout pnlCombobox2Layout = new javax.swing.GroupLayout(pnlCombobox2);
        pnlCombobox2.setLayout(pnlCombobox2Layout);
        pnlCombobox2Layout.setHorizontalGroup(
            pnlCombobox2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCombobox2Layout.createSequentialGroup()
                .addComponent(cbxAno3, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(cbxAno4, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlCombobox2Layout.setVerticalGroup(
            pnlCombobox2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCombobox2Layout.createSequentialGroup()
                .addGroup(pnlCombobox2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxAno4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxAno3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        cbxAno1.setFont(new java.awt.Font("Arial", 0, 12));
        cbxAno1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Y");

        cbxAno2.setFont(new java.awt.Font("Arial", 0, 12));
        cbxAno2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout pnlCombobox1Layout = new javax.swing.GroupLayout(pnlCombobox1);
        pnlCombobox1.setLayout(pnlCombobox1Layout);
        pnlCombobox1Layout.setHorizontalGroup(
            pnlCombobox1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCombobox1Layout.createSequentialGroup()
                .addComponent(cbxAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(cbxAno2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlCombobox1Layout.setVerticalGroup(
            pnlCombobox1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cbxAno1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(cbxAno2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Generar Reporte Gráficos de comparación");
        setMinimumSize(new java.awt.Dimension(503, 246));
        setModal(true);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Generar Graficas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(503, 313));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel4.setText("La graficas seran generadas sobre la entidad accidentes.");

        cbxGenerarReporte.setFont(new java.awt.Font("Arial", 1, 12));
        cbxGenerarReporte.setText("Generar  Grafica");
        cbxGenerarReporte.setBorder(null);
        cbxGenerarReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxGenerarReporteActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("Tipo de Gráfica:");

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setText("Seleccione los años a comparar: ");

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel5.setText("Cantidad de años:");

        buttonGroup1.add(radioDos);
        radioDos.setFont(new java.awt.Font("Arial", 0, 12));
        radioDos.setSelected(true);
        radioDos.setText("2 Años");
        radioDos.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radioDosItemStateChanged(evt);
            }
        });

        buttonGroup1.add(radioVarios);
        radioVarios.setFont(new java.awt.Font("Arial", 0, 12));
        radioVarios.setText("Varios");
        radioVarios.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radioVariosItemStateChanged(evt);
            }
        });

        buttonGroup2.add(rbtnLineal);
        rbtnLineal.setFont(new java.awt.Font("Arial", 0, 12));
        rbtnLineal.setSelected(true);
        rbtnLineal.setText("Lineal");

        buttonGroup2.add(rbtnBarras);
        rbtnBarras.setFont(new java.awt.Font("Arial", 0, 12));
        rbtnBarras.setText("Barras");

        buttonGroup2.add(rbtnTorta);
        rbtnTorta.setFont(new java.awt.Font("Arial", 0, 12));
        rbtnTorta.setText("Torta");

        javax.swing.GroupLayout pnlContenedorComboboxLayout = new javax.swing.GroupLayout(pnlContenedorCombobox);
        pnlContenedorCombobox.setLayout(pnlContenedorComboboxLayout);
        pnlContenedorComboboxLayout.setHorizontalGroup(
            pnlContenedorComboboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 255, Short.MAX_VALUE)
        );
        pnlContenedorComboboxLayout.setVerticalGroup(
            pnlContenedorComboboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 24, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioDos, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rbtnLineal, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioVarios, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(rbtnBarras, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(10, 10, 10)
                                .addComponent(rbtnTorta, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(cbxGenerarReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pnlContenedorCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioDos, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioVarios, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnLineal, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnBarras, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rbtnTorta, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlContenedorCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(cbxGenerarReporte, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
//______________________________________________________________________________

private void cbxGenerarReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxGenerarReporteActionPerformed

    if (radioDos.isSelected()) {
        if (cbxAno1.getSelectedIndex() < 0 || cbxAno2.getSelectedIndex() < 0) {
            JOptionPane.showMessageDialog(this, "Seleccione años a comparar", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            ano1 = Integer.parseInt(cbxAno1.getSelectedItem().toString());
            ano2 = Integer.parseInt(cbxAno2.getSelectedItem().toString());
            if (rbtnLineal.isSelected()) {
                jasper.ejecutarReporteComp(ano1, ano2, "Accidentes (Linea)", "ReportCompaLineal");
            } else if (rbtnBarras.isSelected()) {
                jasper.ejecutarReporteComp(ano1, ano2, "Accidentes (Barras)", "ReportCompaBarra");
            } else if (rbtnTorta.isSelected()) {
                jasper.ejecutarReporteComp(ano1, ano2, "Accidentes (Torta)", "ReportCompaTorta");
            }
        }
    } else if (radioVarios.isSelected()) {
        if (cbxAno3.getSelectedIndex() < 0 || cbxAno4.getSelectedIndex() < 0) {
            JOptionPane.showMessageDialog(this, "Seleccione años a comparar", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            ano1 = Integer.parseInt(cbxAno3.getSelectedItem().toString());
            ano2 = Integer.parseInt(cbxAno4.getSelectedItem().toString());
            jasper.ejecutarReporteComp(ano1, ano2, "Accidentes Generales", "ReportCompaVarios");
        }
    }

}//GEN-LAST:event_cbxGenerarReporteActionPerformed
//______________________________________________________________________________

    private void radioDosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radioDosItemStateChanged

        rbtnLineal.setEnabled(true);
        rbtnBarras.setEnabled(true);
        rbtnTorta.setEnabled(true);
        pnlCombobox1();
        llenarcomobo();

    }//GEN-LAST:event_radioDosItemStateChanged
//______________________________________________________________________________

    private void radioVariosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radioVariosItemStateChanged

        cbxAno1.removeAllItems();
        cbxAno2.removeAllItems();
        rbtnLineal.setEnabled(false);
        rbtnBarras.setEnabled(false);
        rbtnTorta.setEnabled(false);
        pnlCombobox2();

    }//GEN-LAST:event_radioVariosItemStateChanged
//______________________________________________________________________________
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbxAno1;
    private javax.swing.JComboBox cbxAno2;
    private javax.swing.JComboBox cbxAno3;
    private javax.swing.JComboBox cbxAno4;
    private javax.swing.JButton cbxGenerarReporte;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnlCombobox1;
    private javax.swing.JPanel pnlCombobox2;
    private javax.swing.JPanel pnlContenedorCombobox;
    private javax.swing.JRadioButton radioDos;
    private javax.swing.JRadioButton radioVarios;
    private javax.swing.JRadioButton rbtnBarras;
    private javax.swing.JRadioButton rbtnLineal;
    private javax.swing.JRadioButton rbtnTorta;
    // End of variables declaration//GEN-END:variables
}
